<template>
  <el-dialog
    :title="textMap[dialogStatus]"
    :visible.sync="dialogFormVisible2"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :fullscreen="false"
  >
    <el-form
      ref="dataForm"
      :rules="rules"
      :model="formData"
      label-position="left"
      label-width="120px"
      style="width: 500px; margin-left:50px;"
    >
      <el-form-item label="角色名" prop="roleName">
        <el-input v-model="formData.roleName" />
      </el-form-item>
      <el-form-item label="关联app" prop="appList">
        <el-select v-model="formData.selectedAppIdList" multiple placeholder="选择app" filterable class="filter-item" style="width:380px">
          <el-option v-for="item in appList" :key="item.id" :label="item.fullname" :value="item.id" />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible2 = false">
        取消
      </el-button>
      <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
        确认
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import * as apiRole from '@/api/role'
import * as apiApp from '@/api/app'

export default {
  name: 'CreateRole',
  props: ['textMap', 'dialogStatus', 'dialogFormVisible'],
  computed: {
    dialogFormVisible2: {
      get() {
        return this.dialogFormVisible
      },
      set(val) {
        // dialogFormVisible改变由父组件控制
        this.$emit('update:dialogFormVisible', val)
      }
    }
  },
  created() {
    this.getAppList()
  },
  data() {
    return {
      formData: {
        id: undefined,
        roleName: undefined,
        selectedAppIdList: []
      },
      appList: [],
      rules: {
        username: [{ required: true, message: '姓名必填', trigger: 'blur' }],
        password: [{ required: true, message: '密码必填', trigger: 'blur' }],
        shopIdString: [{ required: true, message: '门店必填', trigger: 'blur' }],
        duration: [{ required: true, message: '持续时间必填', trigger: 'blur' }],
        type: [{ required: true, message: 'ip类型必填', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async getAppList() {
      const { content }=await apiApp.getList({ // 查询列表时的参数对象
        pageNumber: 1,
        pageSize: 500
      })
      this.appList = content.list
    },
    clearFormData() {//清除 this.formData数据
      Object.keys(this.formData).forEach(key => {
        this.formData[key] = undefined
      })
    },
    setCurrentRow(currentRowData) {// 修改 某条记录时 获取记录信息
      this.formData.id = currentRowData.id
      this.formData.roleName = currentRowData.roleName
      this.formData.selectedAppIdList = []
      const appList = currentRowData.appList
      appList.forEach((app)=>{
        this.formData.selectedAppIdList.push(app.id)
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          apiRole.createOrUpdate(this.formData).then(() => {
            // 通知父组件刷新列表数据
            this.$emit('on-fresh-list')
            this.dialogFormVisible2 = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.formData)
          apiRole.createOrUpdate(tempData).then(() => {
            // 通知父组件刷新列表数据
            this.$emit('on-fresh-list')
            this.dialogFormVisible2 = false
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
